import 'package:flutter/material.dart';

import '../pages/homePage.dart';
import '../pages/myPage.dart';
import '../pages/searchPage.dart';
import '../pages/tripPage.dart';
import '../util/navigate_util.dart';

class TabNavigator extends StatefulWidget {
  const TabNavigator({super.key});

  @override
  State<TabNavigator> createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> {
  //创建一个controller
  int _currentIndex = 0;
  final PageController _controller = PageController();
  @override
  Widget build(BuildContext context) {
    NavigatorUtil.upgradeContext(context);
    return Scaffold(
      body: PageView(
        physics: NeverScrollableScrollPhysics(),
        controller: _controller,
        children: [Homepage(), Searchpage(), Trippage(), Mypage()],
      ),
      bottomNavigationBar: BottomNavigationBar(
        //指定默认颜色
        fixedColor: Colors.blue,
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (currentIndex) {
          setState(() {
            _currentIndex = currentIndex;
            _controller.jumpToPage(_currentIndex);
          });
        },
        items: [
          tabItem("首页", Icons.home, 0),
          tabItem("搜索", Icons.search, 1),
          tabItem("旅拍", Icons.camera_alt, 2),
          tabItem("我的", Icons.account_circle, 3),
        ],
      ),
    );
  }

  tabItem(String label, IconData icon, int currentIndex) {
    return BottomNavigationBarItem(
      label: label,
      icon: Icon(icon, color: Colors.grey),
      activeIcon: Icon(icon, color: Colors.blue),
    );
  }
}
